package com.homelock.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.homelock.constants.OpmodeEnum;
import com.homelock.domain.UserUnlockPostForm;
import com.homelock.entity.HlDevUserUnlock;
import com.homelock.service.HlDevUserUnlockService;
import com.homelock.vo.LockUserUnlockVo;
import com.pi.base.dto.result.AppResult;
import com.pi.http.base.controller.BaseController;

@RequestMapping("/lock/opmode")
@RestController
public class LockUserUnlockController extends BaseController {

    private HlDevUserUnlockService devUserUnlockService;

    @Autowired
    public LockUserUnlockController(HlDevUserUnlockService devUserUnlockService) {
        this.devUserUnlockService = devUserUnlockService;
    }

    @RequestMapping("/add")
    public AppResult addUserUnlock(UserUnlockPostForm userUnlockPostForm) {
        OpmodeEnum opmodeEnum = OpmodeEnum.getOpmodeEnum(userUnlockPostForm.getOpmode());
        if (OpmodeEnum.UNLOCK_FINGER != opmodeEnum) {
            userUnlockPostForm.setUserId(0L);
        }
        return AppResult.newSuccessResult(devUserUnlockService.addUserUnlock(userUnlockPostForm));
    }

    @RequestMapping("/remove")
    public AppResult removeUserUnlock(Long devId, Long opmodeId) {
        devUserUnlockService.removeUserUnlock(devId, opmodeId);
        return AppResult.OK;
    }

    @RequestMapping("/update/name")
    public AppResult updateUserUnlockName(Long devId, Long opmodeId, String unlockName) {
        devUserUnlockService.updateUserUnlockName(devId, opmodeId, unlockName);
        return AppResult.OK;
    }

    @RequestMapping("/update/attribute")
    public AppResult switchAlarmAttribute(Long devId, Long opmodeId, Integer attribute) {
        devUserUnlockService.switchAlarmAttribute(devId, opmodeId, attribute);
        return AppResult.OK;
    }

    @RequestMapping("/list")
    public AppResult queryUserUnlock(Long devId, Long userId, Integer opmode) {
        OpmodeEnum opmodeEnum = OpmodeEnum.getOpmodeEnum(opmode);
        if (OpmodeEnum.UNLOCK_FINGER != opmodeEnum) {
            userId = 0L;
        }
        List<HlDevUserUnlock> devUnlockList = devUserUnlockService.queryUnlockInfo(devId, opmode, userId);
        List<LockUserUnlockVo> list = new ArrayList<>(0);
        if (CollectionUtils.isEmpty(devUnlockList)) {
            list = new ArrayList<>(0);
        } else {
            list = new ArrayList<>(devUnlockList.size());
            for (HlDevUserUnlock devUserUnlock : devUnlockList) {
                LockUserUnlockVo vo = new LockUserUnlockVo();
                vo.setOpmodeId(devUserUnlock.getId().toString());
                vo.setOpmodeName(devUserUnlock.getUnlockName());
                vo.setOpTime(devUserUnlock.getCreateDate().getTime() / 1000);
                vo.setUnlockAttribute(devUserUnlock.getUnlockAttribute().intValue());
                vo.setUnlockId(devUserUnlock.getUnlockId());
                list.add(vo);
            }
        }
        return AppResult.newSuccessResult(list);
    }
}
